package com.yitongmao.fragment;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.RefreshLayout.PullRefreshLayout;
import com.yitongmao.adapter.ClassHuanCunAdapter;
import com.yitongmao.adapter.ClassRightHCAdapter;
import com.yitongmao.adapter.ClassRightNetWorkOnclickAdapter;
import com.yitongmao.adapter.ClassfitListViewAdapter;
import com.yitongmao.adapter.ClissfitRightListView;
import com.yitongmao.bean.ClassItemRightBean;
import com.yitongmao.bean.ClassfyHotRightBean;
import com.yitongmao.bean.ClassfyLeftBean;
import com.yitongmao.bean.ClasshuancunBean;
import com.yitongmao.bean.ClassrightHuanCunBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.yitmfruit.R;

import org.litepal.crud.DataSupport;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class ClassifyFragment extends Fragment {
    private ListView mLeftListView;
    private ListView mRightRecyclerView;
    private int a = 1;
    private List<ClassfyHotRightBean.PopularListBean> popularList;
    private ClassfitListViewAdapter viewAdapter;
    private boolean flag = false;
    private ClassfyLeftBean classfyLeftBean;
    private PullRefreshLayout layout;

    public ClassifyFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, container, false);
        initView(view);//找到控件
        setmLeftListViewData();//给左边的listview赋值
        setNetWorkmoren();
        setLeftNetWorkList();
        return view;
    }

    public void initView(View view) {
        layout = (PullRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        refushLoading();//listview刷新加载
        mLeftListView = (ListView) view.findViewById(R.id.mLeftListView);
        mRightRecyclerView = (ListView) view.findViewById(R.id.mRightRecyclerView);
        if (!isNetworkConnected(getActivity())) {
            List<ClasshuancunBean> all = DataSupport.findAll(ClasshuancunBean.class);
            mLeftListView.setAdapter(new ClassHuanCunAdapter(getActivity(), all));
            List<ClassrightHuanCunBean> popularList = DataSupport.findAll(ClassrightHuanCunBean.class);
            mRightRecyclerView.setAdapter(new ClassRightHCAdapter(getActivity(), popularList));
            flag = false;
        } else {
            flag = true;
        }
    }

    //刷新加载的方法
    public void refushLoading() {
        //刷新的方法
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //llSearch.setVisibility(View.GONE);
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        //加载
        layout.setOnLoadListener(new PullRefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      /* *//* a++;
                        setNetWork(a,2);*/
                        layout.setLoading(false);
                    }
                }, 1000);
            }
        });
    }

    //给左边的listview赋值
    public void setmLeftListViewData() {
        mLeftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                viewAdapter.changeSelected(position);
                if (position == 0) {
                    setNetWorkmoren();
                } else {
                    int id1 = classfyLeftBean.getClassNameList.get(position).id;
                    setRightNetWorkOnclick(id1);
                }
            }
        });
        mLeftListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                viewAdapter.changeSelected(position);//刷新
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    //二级列表默认
    public void setNetWorkmoren() {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("showNumber", "10");
        params.put("page", "1");
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.classfyhotUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                ClassfyHotRightBean classfyHotRightBean = gson.fromJson(string, ClassfyHotRightBean.class);
                popularList = classfyHotRightBean.popularList;
                if (popularList.equals("") || popularList.size() < 0) {
                    Log.e("TAG", "请求失败goodlist数据为空");
                } else {
                    a++;
                    if (a == 2) {
                        for (int i = 0; i < popularList.size(); i++) {
                            ClassrightHuanCunBean classrightHuanCunBean = new ClassrightHuanCunBean();
                            classrightHuanCunBean.setThumb(popularList.get(i).thumb);
                            classrightHuanCunBean.setGoodsName(popularList.get(i).goodsName);
                            classrightHuanCunBean.setDescribes(popularList.get(i).describes);
                            classrightHuanCunBean.setRetailPrice(popularList.get(i).retailPrice);
                            classrightHuanCunBean.save();
                        }
                    }
                    mRightRecyclerView.setAdapter(new ClissfitRightListView(getActivity(), popularList));
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }

    //左边默认
    public void setLeftNetWorkList() {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.classfyleftUrl, params, new CallBackListener() {
            private ClasshuancunBean classhuancunBean;

            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("getClassName", "" + string);
                Gson gson = new Gson();
                classfyLeftBean = gson.fromJson(string, ClassfyLeftBean.class);
                List<ClassfyLeftBean.GetClassNameListBean> getClassNameList = classfyLeftBean.getClassNameList;
                if (getClassNameList.size() < 0 && getClassNameList == null) {
                    Log.e("TAG", "分类列表数据为空");
                } else {
                    a++;
                    if (a == 3) {
                        for (int i = 0; i < getClassNameList.size(); i++) {
                            classhuancunBean = new ClasshuancunBean();
                            classhuancunBean.setName(getClassNameList.get(i).name);
                            classhuancunBean.save();
                        }
                    }
                    viewAdapter = new ClassfitListViewAdapter(getActivity(), getClassNameList);
                    mLeftListView.setAdapter(viewAdapter);
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }

    //点击item不是0的item切换数据
    public void setRightNetWorkOnclick(int id) {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("page", "1");
        params.put("showNumber", "8");
        params.put("id", String.valueOf(id));
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.classfyonclickUrl, params, new CallBackListener() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(Object result) {
                String string = (String) result;
                Log.e("Gson", "" + string);
                Gson gson = new Gson();
                ClassItemRightBean classItemRightBean = gson.fromJson(string, ClassItemRightBean.class);
                List<ClassItemRightBean.ClassListBean> popularList = classItemRightBean.classList;
                if (popularList.size() < 0 && popularList == null) {
                    Log.e("TAG", "分类列表数据为空");
                } else {
                    mRightRecyclerView.setAdapter(new ClassRightNetWorkOnclickAdapter(getActivity(), popularList));
                }
            }

            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }

    //判断网络
    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
